//
//  BoundingSphere.cpp
//  EidoTron
//
//  Created by Luca Di Franco on 23/06/11.
//  Copyright 2011 Luca Di Franco. All rights reserved.
//

#include "BoundingSphere.h"

#include <algorithm>
#include <GLUT/GLUT.h>

#include "SceneNode.h"
#include "../Entities/Entity.h"

using std::max;

BoundingSphere::BoundingSphere(SceneNode* node) : Collider(node)
{
	float* minMax = node->data->minMaxVal();
	radius = minMax[3] - minMax[0];
	radius = max(radius, minMax[4] - minMax[1]);
	radius = max(radius, minMax[5] - minMax[2]);
	delete minMax;
	radius *= 0.5f;
}

BoundingSphere::~BoundingSphere()
{ }

bool BoundingSphere::checkCollision(Collider* other)
{
	BoundingSphere* otherSphere = dynamic_cast<BoundingSphere*>(other);
	if (otherSphere)
		return checkBSCollision(otherSphere);
	return false;
}

void BoundingSphere::renderCollisionSolid()
{
	glutWireSphere(radius, 16, 16);
}

bool BoundingSphere::checkBSCollision(BoundingSphere* other)
{
	return verticesDistance(relatedNode->lastKnownPosition, other->relatedNode->lastKnownPosition) < radius + other->radius ? true : false; 
}